package com.governtalk.backend.repository;

import com.governtalk.backend.entity.LearningResource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LearningResourceRepository extends JpaRepository<LearningResource, Long> {
    
    Page<LearningResource> findByIsPublicTrueAndStatus(LearningResource.ResourceStatus status, Pageable pageable);
    
    Page<LearningResource> findByTitleContainingIgnoreCaseAndIsPublicTrue(String keyword, Pageable pageable);
    
    Page<LearningResource> findByCategoryIdAndIsPublicTrue(Long categoryId, Pageable pageable);
    
    List<LearningResource> findByIsRecommendedTrueAndIsPublicTrueOrderByCreatedAtDesc(Pageable pageable);
    
    List<LearningResource> findByIsOfficialTrueAndIsPublicTrueOrderByCreatedAtDesc(Pageable pageable);
    
    @Query("SELECT r FROM LearningResource r WHERE r.isPublic = true AND r.status = 'APPROVED' ORDER BY r.downloadCount DESC")
    List<LearningResource> findHotResources(Pageable pageable);
    
    @Query("SELECT r FROM LearningResource r WHERE r.isPublic = true AND r.status = 'APPROVED' ORDER BY r.createdAt DESC")
    List<LearningResource> findNewResources(Pageable pageable);
    
    Page<LearningResource> findByUserId(Long userId, Pageable pageable);
}
